package com.caissa.springboot.starter.msg.service;

import cn.hutool.core.util.IdUtil;
import com.caissa.ecp.base.api.dto.response.JsonResponse;
import com.caissa.retail.msg.export.common.constant.KafkaTopicConstant;
import com.caissa.retail.msg.export.common.enums.MessageTypeEnum;
import com.caissa.retail.msg.export.common.enums.ResendTimeEnum;
import com.caissa.retail.msg.export.po.MsgInfoPo;
import com.caissa.retail.msg.export.po.wechat.qy.*;
import com.base.springboot.starter.kafka.service.KafkaService;
import com.base.springboot.starter.msg.config.ResendPo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.Date;

/**
 * 消息Service
 *
 * @author yuzhongbo
 * @date 2020/5/20
 */
@Slf4j
public class WxQyService {

    @Autowired
    private KafkaService kafkaService;

    /**
     * 发送企业微信欢迎消息
     *
     * @param wxQyWelcomeMsgPo
     * @return
     */
    public ListenableFuture<SendResult<String, Object>> sendWxQyWelcomeMsg(WxQyWelcomeMsgPo wxQyWelcomeMsgPo, ResendPo resendPo) {
        MsgInfoPo msgInfoPo = new MsgInfoPo();
        msgInfoPo.setId(IdUtil.simpleUUID());
        msgInfoPo.setMessageType(MessageTypeEnum.WX_QY_WELCOME);
        msgInfoPo.setSendTotalNum(resendPo.getSendTotalNum());
        msgInfoPo.setResendTime(resendPo.getResendTimeEnum());
        msgInfoPo.setSendDateTime(new Date());
        msgInfoPo.setMessageInfo(wxQyWelcomeMsgPo);
        log.info("sendWxQyWelcomeMsg:{}-{} ",KafkaTopicConstant.SEND_WX_QY_WELCOME_TOPIC, msgInfoPo);
        return kafkaService.send(KafkaTopicConstant.SEND_WX_QY_WELCOME_TOPIC, msgInfoPo);
    }

    /**
     *发送企业微信文本消息
     * @param wxQyAgentTextMsgPo
     * @param resendPo
     * @return
     */
    public ListenableFuture<SendResult<String, Object>> sendWxQyAgentTextMsg(WxQyAgentTextMsgPo wxQyAgentTextMsgPo,  ResendPo resendPo) {
        MsgInfoPo msgInfoPo = new MsgInfoPo();
        msgInfoPo.setId(IdUtil.simpleUUID());
        msgInfoPo.setMessageType(MessageTypeEnum.WX_QY_AGENT_TEXT);
        msgInfoPo.setSendTotalNum(resendPo.getSendTotalNum());
        msgInfoPo.setResendTime(resendPo.getResendTimeEnum());
        msgInfoPo.setSendDateTime(new Date());
        msgInfoPo.setMessageInfo(wxQyAgentTextMsgPo);
        log.info("sendWxQyAgentTextMsg:{}-{} ",KafkaTopicConstant.SEND_WX_QY_AGENT_TOPIC, msgInfoPo);
        return kafkaService.send(KafkaTopicConstant.SEND_WX_QY_AGENT_TOPIC, msgInfoPo);
    }

    /**
     *发送企业微信卡片消息
     * @param wxQyAgentTextcardMsgPo
     * @param resendPo
     * @return
     */
    public ListenableFuture<SendResult<String, Object>> sendWxQyAgentTextcardMsg(WxQyAgentTextcardMsgPo wxQyAgentTextcardMsgPo,  ResendPo resendPo) {
        MsgInfoPo msgInfoPo = new MsgInfoPo();
        msgInfoPo.setId(IdUtil.simpleUUID());
        msgInfoPo.setMessageType(MessageTypeEnum.WX_QY_AGENT_TEXTCARD);
        msgInfoPo.setSendTotalNum(resendPo.getSendTotalNum());
        msgInfoPo.setResendTime(resendPo.getResendTimeEnum());
        msgInfoPo.setSendDateTime(new Date());
        msgInfoPo.setMessageInfo(wxQyAgentTextcardMsgPo);
        log.info("sendWxQyAgentTextcardMsg:{}-{} ",KafkaTopicConstant.SEND_WX_QY_AGENT_TOPIC, msgInfoPo);
        return kafkaService.send(KafkaTopicConstant.SEND_WX_QY_AGENT_TOPIC, msgInfoPo);
    }

    /**
     *发送企业微信任务卡消息
     * @param wxQyAgentTaskcardMsgPo
     * @param resendPo
     * @return
     */
    public ListenableFuture<SendResult<String, Object>> sendWxQyAgentTaskcardMsg(WxQyAgentTaskcardMsgPo wxQyAgentTaskcardMsgPo,  ResendPo resendPo) {
        MsgInfoPo msgInfoPo = new MsgInfoPo();
        msgInfoPo.setId(IdUtil.simpleUUID());
        msgInfoPo.setMessageType(MessageTypeEnum.WX_QY_AGENT_TASKCARD);
        msgInfoPo.setSendTotalNum(resendPo.getSendTotalNum());
        msgInfoPo.setResendTime(resendPo.getResendTimeEnum());
        msgInfoPo.setSendDateTime(new Date());
        msgInfoPo.setMessageInfo(wxQyAgentTaskcardMsgPo);
        log.info("sendWxQyAgentTaskcardMsg:{}-{} ",KafkaTopicConstant.SEND_WX_QY_AGENT_TOPIC, msgInfoPo);
        return kafkaService.send(KafkaTopicConstant.SEND_WX_QY_AGENT_TOPIC, msgInfoPo);
    }

    /**
     *发送企业微信应用消息
     * @param wxQyAgentNewsMsgPo
     * @param resendPo
     * @return
     */
    public ListenableFuture<SendResult<String, Object>> sendWxQyAgentMsg(WxQyAgentNewsMsgPo wxQyAgentNewsMsgPo,  ResendPo resendPo) {
        MsgInfoPo msgInfoPo = new MsgInfoPo();
        msgInfoPo.setId(IdUtil.simpleUUID());
        msgInfoPo.setMessageType(MessageTypeEnum.WX_QY_AGENT_MSG);
        msgInfoPo.setSendTotalNum(resendPo.getSendTotalNum());
        msgInfoPo.setResendTime(resendPo.getResendTimeEnum());
        msgInfoPo.setSendDateTime(new Date());
        msgInfoPo.setMessageInfo(wxQyAgentNewsMsgPo);
        log.info("sendWxQyAgentMsg:{}-{} ",KafkaTopicConstant.SEND_WX_QY_AGENT_TOPIC, msgInfoPo);
        return kafkaService.send(KafkaTopicConstant.SEND_WX_QY_AGENT_TOPIC, msgInfoPo);
    }

    /**
     * 发送企业微信群发消息
     * @param wxQyGroupMsgPo
     * @param resendPo
     * @return
     */
    public ListenableFuture<SendResult<String, Object>> sendWxQyGroupMsgTask(WxQyGroupMsgPo wxQyGroupMsgPo,  ResendPo resendPo) {
        MsgInfoPo msgInfoPo = new MsgInfoPo();
        msgInfoPo.setId(IdUtil.simpleUUID());
        msgInfoPo.setMessageType(MessageTypeEnum.WX_QY_GROUP_MSG_TASK);
        msgInfoPo.setSendTotalNum(resendPo.getSendTotalNum());
        msgInfoPo.setResendTime(resendPo.getResendTimeEnum());
        msgInfoPo.setSendDateTime(new Date());
        msgInfoPo.setMessageInfo(wxQyGroupMsgPo);
        log.info("sendWxQyGroupMsgTask:{}-{} ",KafkaTopicConstant.SEND_WX_QY_GROUP_MSG_TASK_TOPIC, msgInfoPo);
        return kafkaService.send(KafkaTopicConstant.SEND_WX_QY_GROUP_MSG_TASK_TOPIC, msgInfoPo);
    }

}
